package com.lvmama.pangolin.timer.processor;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.hbaseutil.HBaseUtils;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.SpringContextUtil;


public class ExchangeCommodityDataBack  extends CommonJob
{

	@Autowired
	private HBaseUtils hBaseUtils;
	
	private Logger logger = Logger.getLogger(ExchangeCommodityDataBack.class);
	
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		logger.info("转换数据开始....");
		//保存产品信息
		if (hBaseUtils == null)
		{
			hBaseUtils = (HBaseUtils) SpringContextUtil
					.getBean(HBaseUtils.class);
		}
		
		//LMM数据转换
		String commodity = "competition_ticket_commodity_h_lmm";
		String commodity_new = "competition_ticket_commodity_h_lmm_new";
		String price = "competition_ticket_commodity_price_lmm";
		String price_new = "competition_ticket_commodity_price_lmm_new";
		
		String product = "competition_ticket_product_h_lmm";
		String product_new = "competition_ticket_product_h_lmm_new";
		
		logger.info("转换LMM数据开始....");
//		dealExchange(commodity,price,commodity_new,price_new);
		dealExchange(product,product_new);
		logger.info("转换LMM数据结束....");
		
		//TC数据转换
		
		commodity = "competition_ticket_commodity_h_tc";
		commodity_new = "competition_ticket_commodity_h_tc_new";
		price = "competition_ticket_commodity_price_tc";
		price_new = "competition_ticket_commodity_price_tc_new";
		
		product = "competition_ticket_product_h_tc";
		product_new = "competition_ticket_product_h_tc_new";
		logger.info("转换TC数据开始....");
//		dealExchange(commodity,price,commodity_new,price_new);
		dealExchange(product,product_new);
		logger.info("转换TC数据结束....");
		
		//TN数据转换
		commodity = "competition_ticket_commodity_h_tn";
		commodity_new = "competition_ticket_commodity_h_tn_new";
		price = "competition_ticket_commodity_price_tn";
		price_new = "competition_ticket_commodity_price_tn_new";
		
		product = "competition_ticket_product_h_tn";
		product_new = "competition_ticket_product_h_tn_new";
		
		logger.info("转换TN数据开始....");
//		dealExchange(commodity,price,commodity_new,price_new);
		dealExchange(product,product_new);
		logger.info("转换TN数据结束....");
		
		//XC数据转换
		commodity = "competition_ticket_commodity_h_xc";
		commodity_new = "competition_ticket_commodity_h_xc_new";
		price = "competition_ticket_commodity_price_xc";
		price_new = "competition_ticket_commodity_price_xc_new";
		
		product = "competition_ticket_product_h_xc";
		product_new = "competition_ticket_product_h_xc_new";
		
		logger.info("转换XC数据开始....");
//		dealExchange(commodity,price,commodity_new,price_new);
		dealExchange(product,product_new);
		logger.info("转换XC数据结束....");
		
		//QNR数据转换
		commodity = "competition_ticket_commodity_h_qnr";
		commodity_new = "competition_ticket_commodity_h_qnr_new";
		price = "competition_ticket_commodity_price_qnr";
		price_new = "competition_ticket_commodity_price_qnr_new";
		
		product = "competition_ticket_product_h_qnr";
		product_new = "competition_ticket_product_h_qnr_new";
		
		logger.info("转换QNR数据开始....");
//		dealExchange(commodity,price,commodity_new,price_new);
		dealExchange(product,product_new);
		logger.info("转换QNR数据结束....");
		
		logger.info("转换数据结束....");
		
	}
	
	private void dealExchange(String commodityTableName,String priceTableName,String newCommTable,String newPriceTab)
	{
		try
		{
			List<Map<String,String>> result = hBaseUtils.queryAllAndRow(commodityTableName, Constant.HB_VARIABLE_FAMILY);
			if(result != null && !result.isEmpty() )
			{
				for (Map<String, String> map : result)
				{
					String rowkey = map.get("rowkey");
					map.remove("rowkey");
					hBaseUtils.save(map, newCommTable, Constant.HB_VARIABLE_FAMILY, rowkey);
				
				}
				
			}
			List<Map<String,String>> priceResult = hBaseUtils.queryAllAndRow(priceTableName, Constant.HB_VARIABLE_FAMILY);
			if(priceResult != null && !priceResult.isEmpty() )
			{
				for (Map<String, String> map : priceResult)
				{
					String rowkey = map.get("rowkey");
					System.out.println("price row key :" + rowkey);
					map.remove("rowkey");
					hBaseUtils.save(map, newPriceTab, Constant.HB_VARIABLE_FAMILY, rowkey);
				
				}
				
			}
		}catch( Exception e)
		{
			logger.error("转换数据失败！",e);
		}
		
		
		
	}
	
	private void dealExchange(String prodTableName,String newProdTab)
	{
		try
		{
			List<Map<String,String>> result = hBaseUtils.queryAllAndRow(prodTableName, Constant.HB_VARIABLE_FAMILY);
			if(result != null && !result.isEmpty() )
			{
				for (Map<String, String> map : result)
				{
					String rowkey = map.get("rowkey");
					map.remove("rowkey");
					hBaseUtils.save(map, newProdTab, Constant.HB_VARIABLE_FAMILY, rowkey);
				
				}
				
			}
		}catch( Exception e)
		{
			logger.error("转换数据失败！",e);
		}
		
		
		
	}
	
}
